// app.js
var mfApi = require('/api/api.js');

App({
  onLaunch() {
    // 展示本地存储能力
    this.userLogin();

    this.system = wx.getSystemInfoSync();
    setTimeout(() => {
      this.menu = wx.getMenuButtonBoundingClientRect();
      console.log('胶囊信息', this.menu)
    }, 500);

    this.loadFontFile();
    this.globalData.deviceAspectRatio = this.system.windowWidth / this.system.windowHeight;

  },

  loadFontFile() {

    let fontSizeFileData = [{
      name: "LaMerHeadlineWhiteItalic",
      url: "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_lamer_cream_show/font/LaMerHeadline-WhiteItalic.otf"
    }, {
      name: "NeueHaasUnicaProLight",
      url: "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_lamer_cream_show/font/NeueHaasUnicaPro-Light.otf"
    }, ];

    for (let i = 0; i < fontSizeFileData.length; i++) {
      wx.loadFontFace({
        global: true,
        family: fontSizeFileData[i].name,
        scopes: ["native", "webview"],
        source: `url("${fontSizeFileData[i].url}")`,
        success: (e) => {
          console.log("loadFontFace -- ok", e)
        },
        fail: (err) => {
          console.log("loadFontFace -- fail", err)
        },
        complete: (e) => {
          console.log("loadFontFace -- complete", e)
        }
      })
    }
  },

  /**
   * 授权+登录
   */
  userLogin() {
    var _self = this;
    // return;
    //登录
    wx.login({
      success: (e) => {
        console.log("=============jd.login", e)
        _self.wxLoginApi(e.code)
      },
      fail: (res) => {
        console.log("=============jd.login=====fail");
        wx.showToast({
          title: "jdlogin授权失败,请重新进入小程序",
          icon: 'none'
        })
      }
    });
  },


  async wxLoginApi(inCode) {
    let tempOpenData = await mfApi.http(mfApi.getOpenIdByCode(inCode), "POST", {})
    console.log(tempOpenData)
    if (tempOpenData.code != 200) {
      wx.showToast({
        icon: "none",
        title: '后端服务异常',
      })
      return;
    }
    this.globalData.openId = tempOpenData.result.openId;
    mfApi.updateToken(tempOpenData.result.token)

    let infRes = await mfApi.http(mfApi.getUserInfo(this.globalData.openId), "POST", {});
    if (infRes.code == 200) {
      this.globalData.userIcon = infRes.result.avatar;
      this.globalData.userName = infRes.result.nickName;
      if(infRes.result.todayFirstFlag == '1') {
        this.globalData.needFirstFrame = true;
        this.globalData.isBottomEnterExpand = true
      }
    }
  },

  // 埋点
  saveTrack(usercustomType, isInit = false) {
    let inData = {}
    inData.customType = usercustomType;
    inData.version = "v2";
    inData.projectCode = "2224";
    inData.timestamp = new Date().getTime().toString();
    inData.province = "";
    inData.city = "";
    inData.platform = "wx";
    inData.channel = this.globalData.inChannel;
    inData.userId = this.globalData.openId ? this.globalData.openId : "default user";
    if (isInit) {
      inData.type = "flowSource";
      delete inData.customType;
    }
    // return;
    mfApi.http(mfApi.addUserStat(), "POST", inData).then(result => {
      // console.log("addUserStat  --- ok ")
      // console.log(result)
    });

    // console.log("saveTrack:" + usercustomType);
  },

  globalData: {
    openId: null,
    userInfo: null,

    // 当前所选房间
    gCurrentRoomIndex: 1,
    baseUrl: "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_lamer_cream_show",
    baseExitVideoUrl: 'https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/cream/20220418a-m',

    deviceAspectRatio: 0,

    // 全局的判断是否需要方向提示
    // r5未解锁：需要方向提示
    isNeedCheckVideoDirectTip: true,

    // 回到index后需要对准的房门
    indexTurnDoor: -1, //-1,
    // 是不是从查看分享证书回到index
    isFromShareDoor: false,

    // 是否需要每日首次登陆的提示
    // getuserinfo接口返回是否每天首次登陆，启动后会多次调用这个接口，需要记录这个值，index里会用
    needFirstFrame: false,

    // 底部入口默认展开
    isBottomEnterExpand: false,


    // 埋点相关
    inChannel: 'wx',
  }
})